//
//  ViewController.swift
//  ICCLLocationDemo
//
//  Created by cc on 2018/7/19.
//  Copyright © 2018年 cc. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    lazy var locationManager: ICCLLocation = {
       let manager = ICCLLocation()
        weak var weakSelf = self
        manager.locationCompletion = { mod in
            if mod == nil { return }
            print(mod!.name ?? "???")
            weakSelf?.locationLab.text = mod!.province! + mod!.city! + mod!.district! + mod!.street! + mod!.subStreet!
        }
        manager.coordinateCompletion = { mod in
            print(mod.lat ?? "lat")
            print(mod.lon ?? "lon")
//            weakSelf?.locationLab
            
        }
        return manager
    }()
    var locationLab: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        let onceBtn = createBtn("获取一次")
        onceBtn.addTarget(self, action: #selector(onceBtnClick), for: .touchUpInside)
        let awaysBtn = createBtn("持续获取")
        awaysBtn.addTarget(self, action: #selector(awaysBtnClick), for: .touchUpInside)
        locationLab = createLab("位置信息")
        
        
        
        onceBtn.frame = CGRect(x: 20, y: 100, width: 80, height: 50)
        awaysBtn.frame = CGRect(x: view.frame.width - 100, y: onceBtn.frame.minY, width: onceBtn.frame.width, height: onceBtn.frame.height)
        
        locationLab.frame = CGRect(x: 20, y: onceBtn.frame.maxY + 80, width: view.frame.width - 40, height: 50)
    }

    @objc func onceBtnClick() {
        locationManager.useType = .inUse
        locationManager.locationStart()
    }
    @objc func awaysBtnClick() {
        locationManager.useType = .always
        locationManager.locationStart()
    }
    
    func createBtn(_ title: String) -> UIButton {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = .orange
        btn.setTitleColor(.white, for: .normal)
        btn.setTitle(title, for: .normal)
        view.addSubview(btn)
        return btn
    }
    
    func createLab(_ title: String) -> UILabel {
        let lab = UILabel()
        lab.textAlignment = .center
        lab.textColor = .brown
        lab.text = title
        view.addSubview(lab)
        return lab
    }

    func drawCorner(_ v: UIView, _ clip: Bool) {
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = clip
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

